package com.example.controller;

import com.example.pojo.ResultBean;
import com.example.pojo.TypeTemplate;
import com.example.req.TypeTemplateReq;
import com.example.service.TemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @GetMapping("/list")
    public ResultBean list(TypeTemplateReq typeTemplateReq){
        System.out.println("查询请求体：" + typeTemplateReq);
        PageHelper.startPage(typeTemplateReq.getPageNum(),typeTemplateReq.getPageSize());
        List<TypeTemplate> typeTemplates = templateService.queryByCondition(typeTemplateReq);
        PageInfo pageInfo = new PageInfo(typeTemplates);
        return ResultBean.success(pageInfo);
    }

    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody TypeTemplate typeTemplate){
        System.out.println("新增或修改的数据" + typeTemplate);
        int rs = templateService.saveOrUpdate(typeTemplate);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的数据的id："  + id);
        int rs = templateService.deleteById(id);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }
}
